package com.st.qdmall.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.st.qdmall.entity.Admin;
import com.st.qdmall.service.IAdminService;

@Controller
public class AdminController {
	
	@Autowired
	private IAdminService adminService;
	
	
	/**
	 * 提供管理员登录的界面
	 */
	@GetMapping("/admin/login.do")
	public String login() {
		return "admin/login";
	}
	/**
	 * 接收管理员登录的操作
	 */
	@PostMapping("/admin/login.do")
	public ModelAndView login(
			String username,
			String password,
			HttpSession session,
			ModelAndView mv) {
		// 调用Service层
		Admin admin = adminService.login(username, password);
		
		if (admin != null) {
			// 登录成功
			session.setAttribute("admin", admin);
			
			// 跳转到Admin的主页
			mv.setViewName("redirect:/admin/index.do");
		} else {
			// 登录失败
			mv.addObject("error", "账号或密码错误");
			mv.setViewName("admin/login");
		}
		
		return mv;
	}
}






